package com.yinp.proapp.module.wanandroid.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.yinp.proapp.R;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.fragment.PresenterWanAndroidFragment;
import com.yinp.proapp.databinding.DialogOfficialAccountChildBinding;
import com.yinp.proapp.databinding.FragmentWanOfficialAccountBinding;
import com.yinp.proapp.databinding.ItemOfficialAccountListBinding;
import com.yinp.proapp.databinding.ItemWanOfficialAccountBinding;
import com.yinp.proapp.module.wanandroid.bean.WanOfficialAccountBean;
import com.yinp.proapp.module.wanandroid.bean.WanOfficialAccountListBean;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanData2;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanObserver2;
import com.yinp.proapp.utils.JumpWebUtils;
import com.yinp.tools.fragment_dialog.BaseDialogFragment;
import com.yinp.tools.fragment_dialog.CommonDialogFragment;
import com.yinp.tools.fragment_dialog.DialogFragmentHolder;
import com.yinp.tools.fragment_dialog.ViewConvertListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :yinpeng
 * date      :2021/8/27
 * package   :com.yinp.proapp.module.wanandroid.fragment
 * describe  :微信公众号
 */
public class WanOfficialAccountFragment extends PresenterWanAndroidFragment<FragmentWanOfficialAccountBinding> {
    private List<WanOfficialAccountBean> dataList = new ArrayList<>();
    private CommonAdapter<WanOfficialAccountBean> adapter;

    private List<WanOfficialAccountListBean.DatasBean> childList = new ArrayList<>();
    private CommonAdapter<WanOfficialAccountListBean.DatasBean> childAdapter;
    private int childPge = 1;
    private int childId = -1;
    private int preChildId = -2;

    public static WanOfficialAccountFragment getInstance() {
        WanOfficialAccountFragment wanOfficialAccountFragment = new WanOfficialAccountFragment();
        Bundle bundle = new Bundle();
        wanOfficialAccountFragment.setArguments(bundle);
        return wanOfficialAccountFragment;
    }

    @Override
    protected void initViews(View view) {
        initRecycler();
        getOfficialAccount();
    }

    private void initRecycler() {
        adapter = new CommonAdapter<WanOfficialAccountBean>(mContext, dataList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                ItemWanOfficialAccountBinding itemWanSquareBinding = ItemWanOfficialAccountBinding.inflate(mInflater, parent, false);
                return new SingleViewHolder(itemWanSquareBinding);
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, WanOfficialAccountBean item) {
                ItemWanOfficialAccountBinding binding = (ItemWanOfficialAccountBinding) holder.getBinding();
                binding.tvTitle.setText(item.getName());
            }
        };
        adapter.setOnItemClickListener((position, view) -> {
            childPge = 1;
            childId = dataList.get(position).getId();
            getChild(null);
        });
        bd.baseRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        bd.baseRecycle.setHasFixedSize(true);
        bd.baseRecycle.setAdapter(adapter);
    }

    /**
     * 获取导航列表
     */
    private void getOfficialAccount() {
        showLoading("加载中...");
        presenter.getOfficialAccount(new WanObserver2<WanData2<List<WanOfficialAccountBean>>>() {
            @Override
            public void onSuccess(WanData2<List<WanOfficialAccountBean>> o) {
                if (o.getData() != null) {
                    List<WanOfficialAccountBean> list = o.getData();
                    if (list != null && list.size() > 0) {
                        dataList.clear();
                        dataList.addAll(list);
                        adapter.notifyDataSetChanged();
                    } else {
                        showToast("暂无数据");
                    }
                } else {
                    showToast("暂无数据");
                }
                hideLoading();
            }

            @Override
            public void onCodeFail(String msg, int code) {
                hideLoading();
                showToast(msg);
            }
        });
    }

    /**
     * 获取导航列表
     */
    private void getChild(SmartRefreshLayout baseRefresh) {
        showLoading("加载中...");
        presenter.getOfficialAccountList(childId, childPge, new WanObserver2<WanData2<WanOfficialAccountListBean>>() {
            @Override
            public void onSuccess(WanData2<WanOfficialAccountListBean> o) {
                if (o.getData() != null) {
                    List<WanOfficialAccountListBean.DatasBean> list = o.getData().getDatas();
                    if (list != null && list.size() > 0) {
                        if (childPge == 1) {
                            childList.clear();
                        }
                        childList.addAll(list);
                        if (childId != preChildId) {
                            showChild();
                        }
                    } else {
                        showToast("暂无数据");
                    }
                } else {
                    showToast("暂无数据");
                }
                preChildId = childId;
                if (baseRefresh != null) {
                    baseRefresh.finishLoadMore(true);
                }
                hideLoading();
            }

            @Override
            public void onCodeFail(String msg, int code) {
                preChildId = childId;
                if (baseRefresh != null) {
                    baseRefresh.finishLoadMore(false);
//                    baseRefresh = null;
                }
                hideLoading();
                showToast(msg);
            }
        });
    }

    private RecyclerView mChildRecycler;

    /**
     * 显示微信公众号的文章
     */
    private void showChild() {
        CommonDialogFragment.newInstance().setLayout(DialogOfficialAccountChildBinding.inflate(getLayoutInflater()))
                .setViewConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(DialogFragmentHolder holder, BaseDialogFragment dialogFragment, ViewBinding viewBinding) {
                        DialogOfficialAccountChildBinding binding = (DialogOfficialAccountChildBinding) viewBinding;
                        if (mChildRecycler == null) {
                            mChildRecycler = binding.rvList;
                        }
                        if (childAdapter == null) {
                            childAdapter = new CommonAdapter<WanOfficialAccountListBean.DatasBean>(mContext, childList) {
                                @Override
                                protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                                    return new SingleViewHolder(ItemOfficialAccountListBinding.inflate(mInflater, parent, false));
                                }

                                @Override
                                public void onBindItem(SingleViewHolder holder, int position, WanOfficialAccountListBean.DatasBean item) {
                                    ItemOfficialAccountListBinding binding1 = (ItemOfficialAccountListBinding) holder.getBinding();
                                    binding1.tvTitle.setText(item.getTitle());
                                    binding1.tvTime.setText("时间：" + item.getNiceDate());
                                    holder.itemView.setOnClickListener(v -> {
                                        if (!TextUtils.isEmpty(item.getLink())) {
                                            JumpWebUtils.startWebView(mContext, item.getTitle(), item.getLink());
                                        }
                                    });
                                }
                            };
                        }
                        SmartRefreshLayout baseRefresh = holder.getView(R.id.baseRefresh);
                        //为下来刷新添加事件
                        binding.baseRefresh.setEnableRefresh(false);
                        //为上拉加载添加事件
                        binding.baseRefresh.setOnLoadMoreListener(refreshLayout -> {
                            childPge++;
                            getChild(holder.getView(R.id.baseRefresh));
                        });
                        binding.rvList.setLayoutManager(new LinearLayoutManager(getContext()));
                        binding.rvList.setHasFixedSize(true);
                        binding.rvList.setAdapter(childAdapter);
                    }
                }).setGravity(BaseDialogFragment.BOTTOM).setAnimStyle(R.style.BottomDialogAnimation)
                .setPercentSize(0f, 0.8f).show(getChildFragmentManager());
    }

    /**
     * 不销毁的话会导致内存泄漏
     */
    @Override
    public void onPause() {
        childAdapter = null;
        preChildId = -2;
        if (mChildRecycler != null)
            mChildRecycler.setAdapter(null);
        mChildRecycler = null;
        super.onPause();
    }
}
